package com.chen.my_leet._2023_11;

import java.util.Arrays;

/**
 * @Author: ChromeChen
 * @Description:
 * @Date: Created in 22:31 2023/11/6 0006
 * @Modified By:
 */
public class Leet_000 {

    static int count = 1;

    public static void main(String[] args) {

        int n = 5;
        int[][] map = generateMap(n);
        snake(map, n);
    }

    /**
     * 生成 n 维数组
     * @param n
     * @return
     */
    private static int[][] generateMap(int n) {
        int[][] map = new int[n][n];
        for (int i = 0; i < map.length; i++) {
            for (int j = 0; j < map.length; j++) {
                map[i][j] = count++;
            }
        }

        output(map);

        return map;
    }

    /**
     * 输出 n 维数组
     * @param map
     */
    private static void output(int[][] map) {
        Arrays.stream(map).forEach(list -> {
                    Arrays.stream(list).forEach(ele -> System.out.print(String.format("%s ", ele)));
                    System.out.println("\n");
                });
    }

    public static void snake(int[][] map, int n) {
        //     123
        //     456
        //     789

        //     187
        //     296
        //     345

        // map[0][0] = 1;
        // map[1][0] = 2;
        //
        // map[2][0] = 3;
        // map[2][1] = 4;
        //
        // map[2][2] = 5;
        // map[1][2] = 6;
        //
        // map[0][2] = 7;
        // map[0][1] = 8;
        //
        // map[1][1] = 9;

        int count = 0;
        for (int k = 0; k < n / 2; k++) {
            for (int i = k; i < n - 1 - k; i++) {
                map[i][k] = ++count;
            }

            for (int i = k; i < n - 1 - k; i++) {
                map[n - 1 - k][i] = ++count;
            }

            for (int i = n - 1 - k; i > k; i--) {
                map[i][n - 1 - k] = ++count;
            }

            for (int i = n - 1 - k; i > k; i--) {
                map[k][i] = ++count;
            }
        }

        // 当是奇数方阵的时候，要多赋值一次
        if (n % 2 == 1) {
            map[n/2][n/2] = ++count;
        }

        output(map);
    }
}
